package main

import (
	"encoding/json"
	"fmt"
)

type orderItem struct {
	Id    string  `json:"id"`
	Name  string  `json:"name"`
	Price float64 `json:"price"`
}

type Order struct {
	ID         string       `json:"id"`
	Item       []orderItem `json:"item"`
	Quantity   int          `json:"quantity"`
	TotalPrice float64      `json:"total_price"`
}

func main() {

	o := Order{
		ID: "1234",
		Item: []orderItem{
			{
				Id:    "item_1",
				Name:  "learn go",
				Price: 10,
			},
			{
				Id:    "item_2",
				Name:  "review",
				Price: 10,
			},
		},
		Quantity:   3,
		TotalPrice: 30,
	}
	//fmt.Printf("%#v",o)
	b, err := json.Marshal(o)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))
	fmt.Println()
	unMarshall()
}

func unMarshall() {
	var a map[string]interface{}
	s := `{
  "id": "1234",
  "item": [
    {
      "id": "item_1",
      "name": "learn go",
      "price": 10
    },
    {
      "id": "item_2",
      "name": "review",
      "price": 10
    }
  ],
  "quantity": 3,
  "total_price": 30
}`
	err := json.Unmarshal([]byte(s), &a)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n",a)

	b, err := json.Marshal(a)
	if err != nil {
		panic(err)
	}
	//fmt.Println("saa")
	fmt.Println(1,string(b))
	fmt.Println()
}
